from selenium import webdriver
import random
import time
import numpy as np
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
 

def get_ele_time(driver,time,func):
    WebDriverWait(driver,time).until(func)
def auto_write():
    # 给出所需的url和option参数
    url_survey = 'https://www.wjx.cn/vm/hoVYe3V.aspx#'  # 根据需要填写url
    driver = webdriver.Chrome(r'C:\Users\Lenovo\Desktop\program\wenjuanxing\chromedriver_win32\chromedriver.exe')
    #防止被浏览器识别为脚本
    option = webdriver.ChromeOptions()
    option.add_experimental_option('excludeSwitches', ['enable-automation'])
    option.add_experimental_option('useAutomationExtension', False)
    driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
                           {'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
   

    chrome_options = Options()
    chrome_options.add_argument("--disable-blink-features=AutomationControlled")
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

    # chrome_options.add_argument("--headless")
    # chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument(
        'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.5615.86 Safari/537.36')
    
    #driver = Chrome(options=chrome_options)
    driver.get(url_survey)
    time.sleep(0.2)

    # 获取到所有题目的选项
    rank = driver.find_elements_by_class_name("field.ui-field-contain")
    for i in range(len(rank)):
        type_q = rank[i].get_attribute("type")

        # 填空
        if(type_q == '1'):
            #value = rank[i].find_element_by_class_name("ui-input-text")
            #i_v = driver.find_element_by_tag_name('input')
            g_b_id = rank[i].find_element_by_id("q18")
            g_b_id.clear()
            g_b_id.send_keys("无")
        # 单选
        if(type_q == '3'):
            check = rank[i].find_elements_by_class_name("ui-radio")
            mins = 0
            check[mins].click()
        #     多选
        if(type_q == '4'):
            ui_check = rank[i].find_elements_by_class_name("label")
            length = len(ui_check)

            for k in range(len(ui_check)):
                # 如果多选中填空题选项不叫其他,请更改此行代码
                if ui_check[k].text == '其他':
                    length = length-1
            # 如果多选规定选择的项数请修改下面的
            random_choice = np.random.randint(1,length-1,size=random.randint(1,length))
            for z in list(set(random_choice)):
                ui_check[z].click()
        #         量表
        if(type_q == '5'):
            lb_data = rank[i].find_elements_by_class_name("td")
            lb_data[random.randint(0,len(lb_data)-1)].click()
        #     矩阵量表
        if(type_q == '6'):
            # print(rank[i].text)

            mat_data = rank[i].find_elements_by_css_selector("tr[tp=d]")
            # mat_data = rank[i].find_elements_by_xpath("./tbody/tr[@tp=\"d\"]")
            for s in range(0,len(mat_data)):
                single_mat_row = mat_data[s].find_elements_by_class_name('rate-off.rate-offlarge')
                # print(len(single_mat_row))
                single_mat_row[random.randint(0,len(single_mat_row)-1)].click()

    driver.find_element_by_id("ctlNext").click()
    # 消除确认弹窗
    time.sleep(random.randint(2, 5))
    js = 'document.getElementById("alert_box").style.display="none";'
    #driver.execute_script(js)

    # 点智能验证提示框，进行智能验证
    driver.find_element_by_class_name("layui-layer-btn0").click()
    driver.find_element_by_xpath("//div[@id='rectMask']").click()
    #
    time.sleep(random.randint(2, 5))
    driver.quit()  # 运行完毕自动关闭浏览器


    # driver.quit()

if __name__ == '__main__':
    for i in range(0,10):
        print("正在填写第{}份问卷".format(i+1))
        auto_write()
        print("第{}份问卷已经填写完成".format(i+1))
        time.sleep(random.randint(1,4))
